package rent_car.XmlAction;

import rent_car.Utils.Utils;
import rent_car.domain.CarBase;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.util.List;

public class ReadXml {

    //读取xml中的数据，封装到数据库中
    public void paraseXml(){
        SAXReader reader = new SAXReader();
        try {
            Document read = reader.read("books.xml");
            Element root = read.getRootElement();
            List<Element> listElement=root.elements();  //遍历第一层元素

            //遍历所有字元素获取其中的元素，装入数据库中
            for(Element ele:listElement){
                CarBase carBase = new CarBase();
                List<Element> childElement=ele.elements();  //遍历第二层元素
                //System.out.println("ele:"+ele.getName()+":"+ele.attribute("id").getValue());
                carBase.setId(Integer.valueOf(ele.attribute("id").getValue()));

                //System.out.println("    name: "+ele.element("name").getText());
                //System.out.println("    price: "+ele.element("price").getText());
                carBase.setName(ele.element("name").getText());
                carBase.setPrice(Double.valueOf(ele.element("price").getText()));

                if(childElement.contains(ele.element("carry_size"))){
                   // System.out.println("    carry_size: "+ele.element("carry_size").getText());
                    carBase.setcSize(ele.element("carry_size").getText());
                }
                if(childElement.contains(ele.element("person_size"))){
                   // System.out.println("    person_size: "+ele.element("person_size").getText());
                    carBase.setpSize(ele.element("person_size").getText());
                }

                if( Utils.isEmpty("carBase")){ //如果carbase表不为空，执行插入语句
                    Utils.insert(carBase);    //数据库中插入数据，调用DBUtils的insert方法，执行出事化操作
                }

            }
            System.out.println("数据库初始化成功！");
        } catch (DocumentException e) {
            e.printStackTrace();
        }


    }

    //生成result.xml结果
    public static void  createXml(CarBase car, int days, int type, double result) {
        File file = new File("result.xml");
            try {
                if(!file.exists()){
                    file.createNewFile();
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件创建失败！");
            }
        SAXReader reader = new SAXReader();
        Document doc=null;

        try {
          doc= reader.read("result.xml");
        } catch (DocumentException e) {
            e.printStackTrace();
            System.out.println("result.xml文件解析失败！");
        }
        Element root = doc.getRootElement();
        Element newOrder = DocumentHelper.createElement("order");
        Attribute id = DocumentHelper.createAttribute(newOrder, "id", String.valueOf(car.getId()));
        System.out.println("order的id:"+ car.getId());
        newOrder.add(id);
        root.add(newOrder);

        Element name = DocumentHelper.createElement("name");    //创建name
        System.out.println(car.getName());
        name.setText(car.getName());
        newOrder.add(name);

        Element rentDays = DocumentHelper.createElement("rentDays");
        Text rentdaytext = DocumentHelper.createText(String.valueOf(days));
        rentDays.add(rentdaytext);
        newOrder.add(rentDays);

        if(type==1){  //货车
            Element csize = DocumentHelper.createElement("载重量");
            Text csizetext = DocumentHelper.createText(car.getcSize());
            csize.add(csizetext);
            newOrder.add(csize);
        }
        if(type==2){ //客车
            Element psize = DocumentHelper.createElement("载客量");
            Text psizetext = DocumentHelper.createText(car.getpSize());
            psize.add(psizetext);
            newOrder.add(psize);
        }
        if(type==3){ //皮卡
            Element csize = DocumentHelper.createElement("载重量");
            Text csizetext = DocumentHelper.createText(car.getcSize());
            csize.add(csizetext);
            newOrder.add(csize);

            Element psize = DocumentHelper.createElement("载客量");
            Text psizetext = DocumentHelper.createText(car.getpSize());
            psize.add(psizetext);
            newOrder.add(psize);
        }

        Element rentprice = DocumentHelper.createElement("总金额");
        Text rentpricetext = DocumentHelper.createText(String.valueOf(result));
        rentprice.add(rentpricetext);
        newOrder.add(rentprice);


        try {
            //回写操作
            OutputFormat format = OutputFormat.createPrettyPrint();  //创建一个xml书写模板
            XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("result.xml"), format);
            xmlWriter.write(doc);  //将内存中的domcument对象doc写入到xml文件中
            xmlWriter.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("回写失败！");
        }
    }




    public static void main(String[] args){

      //  new ReadXml().createXml();
    }



}
